@extends('backstage.layout.master')
@section('title', '标签下的绑定的模板')
@section('style')
<link rel="stylesheet" href="backstage/assets/css/bootstrap-timepicker.css"/>
<link rel="stylesheet" href="backstage/assets/css/daterangepicker.css"/>
<link rel="stylesheet" href="backstage/assets/css/colorpicker.css"/>
<link rel="stylesheet" href="backstage/assets/css/datepicker.css"/>
@stop
@section('content')
    <style>
        .state li {
            list-style: none;
            float: left;
            padding: 0 20px;
            text-align: center;
            height: 40px;
            line-height: 40px;
        }

        .state ul {
            margin: 0;
        }

        .state a {
            font-size: 12px;
        }
        .che a {
            font-weight: bold;
            color: #151a7c;
        }
    </style>
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/product-temp/product-temp-tag-list">产品模板标签列表</a>
            </li>
            <li>
                标签绑定模板列表
            </li>
        </ul><!-- .breadcrumb -->

        <div class="nav-search" id="nav-search">
        </div><!-- #nav-search -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                产品模板标签列表
                <small>
                    <i class="icon-double-angle-right"></i>
                    标签绑定模板列表
                </small>
            </h1>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <h3 class="header smaller lighter red">标签简介：</h3>
                <div class="well">
                    <h4 class="green smaller lighter">标签参数</h4>
                    1、标签编号：{{isset($tag->id)?$tag->id:""}}</br>
                    2、标签名：{{isset($tag->tag_name)?$tag->tag_name:""}}</br>
                    3、有无绑定子标签：
                    @if(isset($tag->has_sub_tag)&&($tag->has_sub_tag==1))
                     有
                    @elseif(isset($tag->has_sub_tag)&&($tag->has_sub_tag===0))
                      无
                    @else
                    @endif
                     </br>
                    4、当前标签是否获取全部产品模板：
                        @if(isset($tag->tag_type)&&($tag->tag_type==1))
                            是
                        @else
                            不是
                        @endif
                        </br>
                </div>
            </div>
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                @if(isset($tag->tag_type)&&($tag->tag_type==2))
                <div class="alert alert-info">
                    <i class="icon-hand-right"></i>
                    模板添加:&nbsp
                    <select id="sel_gift_type">
                        <option selected value="sel_prod_temp">产品模版</option>
                    </select>
                    <select id="sel_prod_temp"  class="sel_gift_type">
                        @if(sizeof($un_sel_product_temps)==0)
                        <option value="">已经没有可添加的模板</option>
                        @else
                            @foreach($un_sel_product_temps as $temp)
                                <option value="{{isset($temp->product_temp_id) ? $temp->product_temp_id : ''}}">
                                    {{isset($temp->name) ? $temp->name : ''}}({{isset($temp->brand_name) ? $temp->brand_name : ''}})
                                </option>
                            @endforeach
                         @endif
                    </select>
                    <button onclick="addRang('{{isset($tag->id)?$tag->id:''}}')">添加</button>
                </div>
                @endif
                <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th class="center">
                            <label>
                                <input type="checkbox" class="ace">
                                <span class="lbl"></span>
                            </label>
                        </th>
                        <th>产品Id</th>
                        <th>产品名称</th>
                        <th>产品类型</th>
                        <th>品牌</th>
                        <th>排序(从小到大)</th>
                        <th>更新时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>

                    <tbody>
                    @foreach ($product_temps as $product_temp)
                        <tr class="" data-tr-id="{{isset($product_temp->tag_resource_map_id)?$product_temp->tag_resource_map_id:'0'}}">
                            <td class="center">
                                <label>
                                    <input type="checkbox" class="ace">
                                    <span class="lbl"></span>
                                </label>
                            </td>
                            <td>
                                {{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:''}}
                            </td>
                            <td>
                                {{isset($product_temp->product_temp_name)?$product_temp->product_temp_name:''}}
                            </td>
                            <td>
                                {{isset($product_temp->category_name)?$product_temp->category_name:''}}
                            </td>
                            <td>
                                {{isset($product_temp->brand_name)?$product_temp->brand_name:''}}
                            </td>
                            <td style="width:68px;">
                                <input type="number" min=1 step=1 name="sort" value="{{$product_temp->sort}}" class="sort" style="width:50px;">
                                <input type="hidden" value="{{$product_temp->sort}}">
                                <input type="hidden" value="{{$product_temp->tag_resource_map_id}}">
                            </td>
                            <td>
                                {{isset($product_temp->updated_at)?date('Y-m-d H:i:s',$product_temp->updated_at):''}}
                            </td>
                            <td class="center" style="width:68px;">
                                @if($tag->tag_type!=1)
                                <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                    <a title="删除" style="color:#d77600;cursor: pointer;" onclick="deleteProductTempMap({{$product_temp->tag_resource_map_id}})">
                                        <i class="icon-trash bigger-130"></i>
                                    </a>
                                </div>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            <?php echo $product_temps->render(); ?>
                        </div>
                    </div>
                </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script src="backstage/assets/js/date-time/bootstrap-datepicker.min.js"></script>
    <script src="backstage/assets/js/date-time/bootstrap-timepicker.min.js"></script>
    <script src="backstage/assets/js/date-time/moment.min.js"></script>
    <script src="backstage/assets/js/date-time/daterangepicker.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('input[name=date-range-picker]').daterangepicker().prev().on(ace.click_event, function () {
                $(this).next().focus();
            });
            $(".sort").blur(function(){
                var sort = $(this).next().val();
                var newSort = $(this).val();
                var id = $(this).next().next().val();
                var a = $(this);
                console.log('newSort:'+newSort);
                $.ajax({
                    type: "POST",
                    url: "/backstage/product-temp/update-tag-temp-map-sort",
                    data:{"sort":newSort,"id":id},
                    success: function(msg){
                        if(msg.state==1){
                            swal({
                                title: "更新成功！",
                                text: "",
                                type: "success",
                                timer:2000
                            });
                            window.location.reload()
                        }else {
                            swal({
                                title: msg.message,
                                text: "",
                                type: "error",
                                timer:2000
                            });
                        }
                    },
                    error:function(){
                        swal({
                            title: "系统繁忙稍后再试",
                            text: "",
                            type: "error",
                            timer:2000
                        });
                    }
                });
            });
        });
        function deleteProductTempMap(id) {
            swal({
                title: "确定删除？",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true
            },function (){
                $.ajax({
                    type: "post",
                    url: "/backstage/product-temp/delete-tag-temp-map",
                    data:{"id":id},
                    dataType: 'json',
                    success: function(res){
                        if(res.state==1){
                            swal({
                                title:'删除成功！',
                                text: "",
                                type: "success",
                                timer:2000
                            });
                            $('[data-tr-id='+id+']').remove();
                        }else{
                            swal({
                                title: res.message,
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    },
                    error:function(){
                        swal({
                            title: "系统繁忙！",
                            text: "",
                            type: "error",
                            timer:2000
                        });
                    }
                });//ajax
            });
        }
        var state = 0;
        function addRang(th){
            if(state == 0){
                state = 1;
                var resource_id = $("#sel_prod_temp option:selected").val();
                console.log('resource_id:'+resource_id);
                console.log('tag_id:'+th);
                if(resource_id == undefined || resource_id == ''){
                    swal('已经没有可添加的模板～','','warning');
                    state = 0;
                    return ;
                }
                swal('处理中，请稍后','','warning');
                $.ajax({
                    type: "post",
                    url: "/backstage/product-temp/add-tag-temp-map",
                    data:{"tag_id":th,"resource_id":resource_id},
                    dataType: 'json',
                    success: function(res){
                        if(res.state ==1){
                            swal({title:'添加成功！', text: "", type: "success", timer:500});
                            location.reload();
                            return ;
                        }
                        swal(res.message,'','warning');
                    },
                    error:function(){
                        swal('系统繁忙','请稍后重试','warning');
                    }
                });
                state = 0;
            }
        }
        /***
         * 判断不超过三位整数
         * @param num
         * @returns {boolean}
         */
        function checkPositiveInteger(num) {
            var reg = /^\+?[1-9][0-9]{1,3}$ /;
            return reg.test(num);
        }
    </script>
@stop